package cc.momas.mybatis.multidatasource.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Sod-Momas
 * @since 2019.11.06
 **/
public class DynamicDataSourceContextHolder {

    public static final Logger logger = LoggerFactory.getLogger(DynamicDataSourceContextHolder.class);
    private static final ThreadLocal<String> DATASOURCE_CONTEXT_HOLDER = new ThreadLocal<>();

    /**
     * 设置数据源
     */
    public static void setDateSourceType(String type) {
        logger.info("set datasource type:[{}]", type);
        DATASOURCE_CONTEXT_HOLDER.set(type);
    }

    /**
     * 获得数据源
     */
    public static String getDateSourceType() {
        logger.info("fetch datasource type");
        return DATASOURCE_CONTEXT_HOLDER.get();
    }

    /**
     * 清空数据源
     */
    public static void clearDateSourceType() {
        logger.info("clear datasource type");
        DATASOURCE_CONTEXT_HOLDER.remove();
    }
}